package service;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import database.*;
import entity.*;
@SuppressWarnings("unused")

public class Room {
	Connection connect =null;
	PreparedStatement pstm=null;
	private String table="tblroom";
	
	public boolean AddRoom(String name, int type , int status){
		connect = Conn.getConnect();
		if (connect == null)
			System.out.println("null");
		try{
			pstm= connect.prepareStatement("INSERT INTO "+ table+"(name,type,status) VALUES (?,?,?)");
			pstm.setString(1,name);
			pstm.setInt(2, type);
			pstm.setInt(3, status);
			if(pstm.executeUpdate()>0){
				return true;
			}
			
		}catch(SQLException e){
			
		}
		return false;
	}
	
	public boolean DeleteRoom(int id){
		connect = Conn.getConnect();
		try{
			pstm = connect.prepareStatement("DELETE FROM "+this.table+ " WHERE id = ?");
			pstm.setInt(1, id);
			if(pstm.executeUpdate()>0){
				return true;
			}
		}catch(SQLException e){
			
		}
		return false;
	}
	public boolean UpdateRoom(int id, String name, int status, int type){
		connect= Conn.getConnect();
		try{
			pstm = connect.prepareStatement("UPDATE "+this.table +"SET name=?," +
					"type=?,status=?  WHERE id=?");
			pstm.setString(1, name);
			pstm.setInt(2, type);
			pstm.setInt(3, status);
			pstm.setInt(4,id);
			if(pstm.executeUpdate()>0){
				return true;
			}
		}catch(SQLException e){
			
		}
		return false;
	}
	public  ArrayList<entity.Room> GetAllRoom(){
		connect= Conn.getConnect();
		ArrayList<entity.Room> list = new ArrayList<entity.Room>();
		
		ResultSet rs=null;
		try{
			pstm= connect.prepareStatement("Select *from "+this.table);			
			rs=pstm.executeQuery();			
			while(rs.next()){
				entity.Room room = new entity.Room();
				room.setId(rs.getInt("id"));
				room.setName(rs.getString("name"));
				room.setType(rs.getInt("type"));
				room.setStatus(rs.getInt("status"));
				list.add(room);
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
		return list;
	}
	public entity.Room GetRoomById(int id){
		
		connect= Conn.getConnect();
		ResultSet rs=null;
		entity.Room room = null;
		
		try{
			pstm= connect.prepareStatement("Select *from "+this.table+" where id=?");
			pstm.setInt(1, id);
			rs= pstm.executeQuery();
		while(rs.next()){
			room =  new entity.Room();;
			room.setId(rs.getInt("id"));
			room.setName(rs.getString("ten_phong"));
			room.setStatus(rs.getInt("tinh_trang"));
			room.setType(rs.getInt("loai_phong"));
		}
	
		}catch(SQLException e){
			e.printStackTrace();
		}
		return room;
	}
	
	public void GetIdByName(String name){
		connect= Conn.getConnect();
		ResultSet rs=null;
		entity.Room room = new entity.Room();
		try{
			pstm= connect.prepareStatement("Select *from "+this.table+" where ten_phong=?");
			pstm.setString(1,name);
			rs= pstm.executeQuery();
			while(rs.next()){

				room.setId(rs.getInt("id"));
					
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
	}
        public void GetAllRoomByType(String id ){
        
        }
	
}

